package com.lvcoding.sec3.secdemo3.security;

import com.lvcoding.sec3.secdemo3.util.JwtUtil;
import com.lvcoding.sec3.secdemo3.util.ResponseUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @description 描述
 * @date   2022-02-28 下午7:43
 * @author  wuyanshen
 */
@Component
public class LoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("username", authentication.getName());
        String token = JwtUtil.createToken(map);
        ResponseUtil.JsonResult(response, 200, "登录成功", token);
    }

}
